package com.groupseven.backend.controller;

import com.groupseven.backend.domain.PrizeProduct.PrizeProduct;
import com.groupseven.backend.domain.ResultType;
import com.groupseven.backend.domain.User.User;
import com.groupseven.backend.services.PrizeProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/v1/PrizeProduct")
public class PrizeProductController {
    @Autowired
    private PrizeProductService prizeProductService;
    @GetMapping("/")
    public ResultType getallPrizeProduct(){
        List<PrizeProduct> tmp = prizeProductService.getallPrizeProduct();
        ResultType resultType=new ResultType(1,"查询成功",200);
        resultType.setPrizeProducts(tmp);
        return resultType;
    }

    @GetMapping("/ways1/{PrizeProductNumber}")
    public ResultType getPrizeProductByPrizeProductNumber(@PathVariable("PrizeProductNumber") String PrizeProductNumber){
        if(prizeProductService.getPrizeProductByPrizeProductNumber(PrizeProductNumber)==null){
            return new  ResultType(0,"查询失败，获奖成果编号不存在",200);
        }
        else
        {
            PrizeProduct prizeProduct=prizeProductService.getPrizeProductByPrizeProductNumber(PrizeProductNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProduct(prizeProduct);
            return  resultType;
        }
    }

    @PostMapping("/")
    public ResultType addPrizeProduct(@RequestBody PrizeProduct prizeProduct){
        Subject subject= SecurityUtils.getSubject();
        User user=(User)subject.getPrincipal();
        prizeProduct.setUsername(user.Username);
        prizeProduct.setApprovalStatus(2);
        /////////////////////////////////////////////
        SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm:ss:SSS");
        prizeProduct.setPrizeProductNumber(formatter.format(new Date()));
        /////////////////////////////////////////////
        Date date=new Date();
        prizeProduct.setStartTime(date);
        if(prizeProductService.addPrizeProduct(prizeProduct)==-1){
            return new  ResultType(0,"插入失败，获奖成果编号已存在",200);
        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-2){
//            return new  ResultType(0,"插入失败，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-3){
//            return new  ResultType(0,"插入失败，专利成果编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-5){
//            return new  ResultType(0,"插入失败，专利编号不存在，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-6){
//            return new  ResultType(0,"插入失败，专利编号存在，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-7){
//            return new  ResultType(0,"插入失败，专利编号不存在，论文编号存在",200);
//        }
        else if (prizeProductService.addPrizeProduct(prizeProduct)==-2){
            return new  ResultType(0,"插入失败，项目编号已存在",200);
        }
        else{
            return new  ResultType(1,"插入成功",200);
        }
    }

    @PutMapping("/")
    public ResultType updatePrizeProduct(@RequestBody PrizeProduct prizeProduct){
        if(prizeProductService.updatePrizeProduct(prizeProduct)==-1){
            return new  ResultType(0,"修改失败，获奖成果编号已存在",200);
        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-2){
//            return new  ResultType(0,"修改失败，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-3){
//            return new  ResultType(0,"修改失败，专利成果编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-5){
//            return new  ResultType(0,"修改失败，专利成果编号不存在，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-6){
//            return new  ResultType(0,"修改失败，专利编号存在，论文编号不存在",200);
//        }
//        else if (prizeProductService.addPrizeProduct(prizeProduct)==-7){
//            return new  ResultType(0,"修改失败，专利编号不存在，论文编号存在",200);
//        }
        else if (prizeProductService.updatePrizeProduct(prizeProduct)==-2){
            return new  ResultType(0,"插入失败，项目编号已存在",200);
        }
        else{
            return new  ResultType(1,"修改成功",200);
        }
    }

    @DeleteMapping("/")
    public ResultType deleteallPrizeProduct(){
        prizeProductService.deleteallPrizeProduct();
        return new  ResultType(1,"删除成功",200);
    }


    @DeleteMapping("/ways1/{PrizeProductNumber}")
    public ResultType deleteByPrizeProductNumber(@PathVariable("PrizeProductNumber") String PrizeProductNumber){
        if(prizeProductService.deleteByPrizeProductNumber(PrizeProductNumber)==0){
            return new  ResultType(0,"删除失败，获奖编号不存在",200);
        }
        else{
            return new  ResultType(1,"删除成功",200);
        }
    }

    /////////////////////////////////////////////////////
    @PostMapping("/s/")
    public ResultType mutildelPrizeProducts(@RequestBody List<PrizeProduct> PrizeProducts){
        List<String> PrizeProductNumbers=new ArrayList<>();
        for(PrizeProduct prizeProduct:PrizeProducts){
            PrizeProductNumbers.add(prizeProduct.getPrizeProductNumber());
        }

        if(prizeProductService.mutildelPrizeProducts(PrizeProductNumbers)==0){
            return new ResultType(0,"删除失败",200);
        }
        else {
            return new ResultType(1,"删除成功",200);
        }
    }
    ////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways1/{PrizeProductNumber}")
    public ResultType getlikePrizeProductByPrizeProductNumber(@PathVariable("PrizeProductNumber") String PrizeProductNumber){
        if(prizeProductService.getPrizeProductByPrizeProductNumber(PrizeProductNumber)==null){
            return new  ResultType(0,"查询失败，获奖成果编号不存在",200);
        }
        else
        {
            PrizeProduct prizeProduct=prizeProductService.getPrizeProductByPrizeProductNumber(PrizeProductNumber);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProduct(prizeProduct);
            return  resultType;
        }
    }
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways2/{PrizeProductName}")
    public ResultType getlikePrizeProductByPrizeProductName(@PathVariable("PrizeProductName") String PrizeProductName){
        if(prizeProductService.getlikePrizeProductByPrizeProductName(PrizeProductName).isEmpty()){
            return new  ResultType(0,"查询失败，获奖成果名不存在",200);
        }
        else
        {
            List<PrizeProduct> prizeProducts=prizeProductService.getlikePrizeProductByPrizeProductName(PrizeProductName);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
        }
    }
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways3/{username}")
    public ResultType getlikePrizeProductByusername(@PathVariable("username") String username){
        if(prizeProductService.getlikePrizeProductByusername(username).isEmpty()){
            return new  ResultType(0,"查询失败，获奖成果名不存在",200);
        }
        else
        {
            List<PrizeProduct> prizeProducts=prizeProductService.getlikePrizeProductByusername(username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
        }
    }
    /////////////////////////////////////////////////////////////////////
    /////////////////////////////////////////////////////////////////////
    @GetMapping("/likefind/ways4/{PrizeAllauthors}")
    public ResultType getlikePrizeProductByPrizeAllauthors(@PathVariable("PrizeAllauthors") String PrizeAllauthors){
            List<PrizeProduct> prizeProducts=prizeProductService.getlikePrizeProductByPrizeAllauthors(PrizeAllauthors);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
    }
    /////////////////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////
    @GetMapping("/search/{query}")
    public ResultType searchqueryPrizeProduct(@PathVariable String query){
        if(prizeProductService.searchqueryPrizeProduct(query).isEmpty()){
            return new  ResultType(0,"查询失败",200);
        }
        else
        {
            List<PrizeProduct> prizeProducts=prizeProductService.searchqueryPrizeProduct(query);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
        }
    }

/////////////////////////////////////////////////////////

    /////////////////////////////////////////////////////////////////
    @GetMapping("/search/{username}/{query}")
    public ResultType searchuserqueryPrizeProduct(@PathVariable String query,@PathVariable String username){
        if(prizeProductService.searchuserqueryPrizeProduct(query,username).isEmpty()){
            return new  ResultType(0,"查询失败",200);
        }
        else
        {
            List<PrizeProduct> prizeProducts=prizeProductService.searchuserqueryPrizeProduct(query,username);
            ResultType resultType=new ResultType(1,"查询成功",200);
            resultType.setPrizeProducts(prizeProducts);
            return  resultType;
        }
    }

/////////////////////////////////////////////////////////
}
